package com.bcd.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bcd.beans.UserBean;

public class UserManager {	
	private DbManager _db;
	private ArrayList<UserBean> _userList;
	private UserBean _user;
	
	public UserManager(){
		_db = DbManager.getInstance();
	}
	
	public UserBean InitUser(String username, String password){
		SetUser(_db.ExecuteSelect("SELECT * FROM users WHERE user_email = '" + username + "' AND user_password = '" + password + "'"));
	    return _user;
	}
	
	public UserBean GetUser(int user_id) {
		SetUser(_db.ExecuteSelect("SELECT * FROM users WHERE user_id = " + user_id));
		return _user;
	}
	
	public ArrayList<UserBean> GetUserList() {
		_userList = new ArrayList<UserBean>();
		ResultSet rs = _db.ExecuteSelect("SELECT * FROM users");
		while(SetUser(rs)){
			_userList.add(_user);
		}		
		return _userList;
	}
	
	public void SaveUser(UserBean user){
		int num_rows = 0;
		num_rows = _db.ExecuteUpdate("UPDATE users SET " + 
				"user_fname='" + user.getFirstName() + "', " +
				"user_lname='" + user.getLastName() + "', " +
				"user_email='" + user.getEmail() + "', " +
				"user_password='" + user.getPassword() + "', " +
				"user_street='" + user.getStreet() + "', " +
				"user_plz='" + user.getPlz() + "', " +
				"user_city='" + user.getCity() + "', " +
				"user_country='" + user.getCountry() + "' " +
				"WHERE user_id=" + user.getId());
		System.out.println("save_user: num_rows " + num_rows);
	}
	
	public void SaveNewUser(UserBean user){
		int num_rows = 0;
		num_rows = _db.ExecuteUpdate("UPDATE users SET " + 
				"user_fname='" + user.getFirstName() + "', " +
				"user_lname='" + user.getLastName() + "', " +
				"user_email='" + user.getEmail() + "', " +
				"user_password='" + user.getPassword() + "', " +
				"user_street='" + user.getStreet() + "', " +
				"user_plz='" + user.getPlz() + "', " +
				"user_city='" + user.getCity() + "', " +
				"user_country='" + user.getCountry() + "' " +
				"WHERE user_id=" + user.getId());
		System.out.println("save_user: num_rows " + num_rows);
	}
	
	public void DeleteUser(int user_id){}
	
	private boolean SetUser(ResultSet rs){
		ResultSet is_admin;
		_user = new UserBean();
		try {
			if(rs.next()){
				_user.setLogin(true);
				_user.setId(rs.getInt("user_id"));
				_user.setFirstName(rs.getString("user_fname"));
				_user.setLastName(rs.getString("user_lname"));
				_user.setStreet(rs.getString("user_street"));
				_user.setPlz(rs.getString("user_plz"));
				_user.setCity(rs.getString("user_city"));
				_user.setCountry(rs.getString("user_country"));
				_user.setEmail(rs.getString("user_email"));	
				_user.setPassword(rs.getString("user_password"));
				is_admin = _db.ExecuteSelect("SELECT * FROM admins WHERE users_user_id = " + _user.getId());
				if(is_admin.next()){
					_user.setAdmin(true);
				}
				return true;
			}
		} catch (SQLException e) {
			System.out.println("There was an error setting the user!" + e.getMessage());
		}
		return false;
	}
	
	
}
